package com.baikeyang.app.controller.api.ActivityTools.v1;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baikeyang.common.bean.ResponseResult;
import com.baikeyang.common.constant.ExecuteConstants;
import com.baikeyang.service.activity.IActivityFileLibraryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Random;

/**
 * @Author     : BaiKeyang
 * @Date       : Created in 2020/5/25 15:41
 * @Description:
 * @Modified By: 
 * @Version: $
 */
@RestController("activityToolsImg")
@RequestMapping(value = "app/v1/activityTools/img")
public class ImgController {

    @Autowired
    private IActivityFileLibraryService activityFileLibraryService;

    static final String URL = "https://pixabay.com/api/?key=";
    static final String API_KEY = "16726550-3dabe0cd680611df2f42b220a";  // Key
    static final String APP_SECRET = "771e781707bda321a2667920f1a4f222";  // 小程序密钥

    @RequestMapping(value = "random")
    public Object getImg () {
        Random r = new Random();
        String[] _categorys = new String[]{"nature","travel"};
        String q = "山";
        int per_page = 6;
        String category = _categorys[r.nextInt(2)];
        String image_type = "photo";
        int min_width = 1240;
        int min_height = 632;
        String _u = URL+API_KEY+"&q="+q+"&per_page="+per_page+"&category="+category+
                "&image_type=" + image_type + "&min_width=" + min_width + "&min_height=" + min_height;
        String res = HttpUtil.get(_u);
        JSONObject object = JSON.parseObject(res);
        if(null != object && object.containsKey("hits")){
            JSONArray ja = (JSONArray)object.get("hits");
            return ja.get(r.nextInt(7));
        }
        return object;
    }

    @RequestMapping(value = "file/update", method = RequestMethod.POST)
    public ResponseResult fileUpdateForWx(@RequestParam(value = "file") MultipartFile multipartFile,
                                     @RequestParam(value = "userId", required = false) String userId,
                                        HttpServletRequest request) {
        System.out.println(multipartFile);
        System.out.println(userId);
        return activityFileLibraryService.fileUpdateForWx(multipartFile, userId, request);
        //return new ResponseResult(ExecuteConstants.EXECUTESTATUS.成功, ExecuteConstants.EXECUTEINFO.成功, ExecuteConstants.EXECUTECODE.CODE_200);
    }

}
